package com.dk.wiki.service;

import com.dk.wiki.domain.Ebook;
import com.dk.wiki.domain.EbookExample;
import com.dk.wiki.mapper.EbookMapper;
import com.dk.wiki.req.EbookQueryReq;
import com.dk.wiki.req.EbookSaveReq;
import com.dk.wiki.resp.EbookQueryResp;
import com.dk.wiki.resp.PageResp;
import com.dk.wiki.util.SnowFlake;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.dk.wiki.util.CopyUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author: ldk
 * 描述: TODO
 */
@Service
public class EbookService {

    private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);

    @Resource // jdk所带的注解
    // @Autowired springboot所带的注解
    private EbookMapper ebookMapper;

    @Resource
     private SnowFlake snowFlake;

    // 根据单一参数，返回查询数据
    // public List<Ebook> list(String name){
    //     EbookExample ebookExample = new EbookExample();
    //     EbookExample.Criteria criteria = ebookExample.createCriteria();
    //     criteria.andNameLike("%" + name + "%");
    //     List<Ebook> ebooks = ebookMapper.selectByExample(ebookExample);
    //     return ebooks;
    // }

    public PageResp<EbookQueryResp> list(EbookQueryReq ebookQueryReq){
        EbookExample ebookExample = new EbookExample();
        EbookExample.Criteria criteria = ebookExample.createCriteria();
        if (!ObjectUtils.isEmpty(ebookQueryReq.getName())){
            criteria.andNameLike("%" + ebookQueryReq.getName() + "%");
        }
        if (!ObjectUtils.isEmpty(ebookQueryReq.getCategoryId2())){
            criteria.andCategory2IdEqualTo(ebookQueryReq.getCategoryId2() );
        }
        PageHelper.startPage(ebookQueryReq.getPage(), ebookQueryReq.getSize());
        List<Ebook> ebookList = ebookMapper.selectByExample(ebookExample);

        PageInfo<Ebook> pageInfo = new PageInfo<>(ebookList);
        LOG.info("总行数：{}",pageInfo.getTotal());
        LOG.info("总页数：{}",pageInfo.getPages());
        // List<EbookResp> ebookRespList = new ArrayList<>();
        // for (Ebook ebook : ebooksList) {
        //     // EbookResp ebookResp = new EbookResp();
        //     // // 逐一更行赋值，比较慢
        //     // // ebookResp.setId(ebook.getId());
        //     // // 使用spring的BeanUtils工具
        //     // BeanUtils.copyProperties(ebook, ebookResp);
        //     EbookResp ebookResp = CopyUtil.copy(ebook, EbookResp.class);
        //     ebookRespList.add(ebookResp);
        // }

        // 列表复制
        List<EbookQueryResp> ebookQueryRespList = CopyUtil.copyList(ebookList, EbookQueryResp.class);
        PageResp<EbookQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(ebookQueryRespList);
        return pageResp;
    }

    /**
     * 保存
     * @param ebookSaveReq
     */
    public  void save(EbookSaveReq ebookSaveReq){
        Ebook ebook = CopyUtil.copy(ebookSaveReq, Ebook.class);
        if (ObjectUtils.isEmpty(ebookSaveReq.getId())){
            // 新增
            ebook.setId(snowFlake.nextId());
            ebookMapper.insert(ebook);
        }else{
            // 更新
            ebookMapper.updateByPrimaryKey(ebook);
        }
    }

    /**
     * 删除
     * @param id
     */
    public  void delete(Long id){
        ebookMapper.deleteByPrimaryKey(id);
    }

}
